package com.sea.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.sea.wechat.request.po.MESSAGE_TYPE;
import com.sea.wechat.request.po.Text;
import com.sea.wechat.request.po.TextMessage;
import com.sea.wechat.util.ConfigUtil;
import com.sea.wechat.util.HttpUtils;
import com.sea.wechat.util.JedisUtil;
import com.sea.wechat.util.RedisKey;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**   
 * @ClassName:     CustomServiceController   
 * @Description:   客服接口  
 * @author:        wl
 * @date:          2017年8月1日 下午2:26:04     
 */  
@RestController
public class CustomServiceController {

	private static final Logger logger = LoggerFactory.getLogger(CustomServiceController.class);
	
	@Autowired
	private ConfigUtil configUtil;

	@RequestMapping(value="send")
	public String sendMessage(String content,String touser){
		if(StringUtils.isBlank(content)){
			return "消息不能为空";
		}else if (StringUtils.isBlank(touser)) {
			return "接收者不能为空";
		}else {
			//组织发送内容
			TextMessage message = new TextMessage();
			message.setTouser(touser);
			message.setMsgtype(MESSAGE_TYPE.文本.getType());
			Text text = new Text();
			text.setContent(content);
			message.setText(text);
			String url = configUtil.getCustomMessageSendUrl()+ JedisUtil.get(RedisKey.ACCESS_TOKEN);
			Map<String, String> head = new HashMap<String, String>();
			head.put("Content-Type", "application/json");
			String result = HttpUtils.HttpsClientPost(url, head, null, JSONObject.toJSONString(message));
			logger.info("sendMessage发送消息:"+JSONObject.toJSONString(message)+"=====sendMessage返回结果："+result);
		}
		return "success";
	}
	
}
